package com.coco.homework.rules;

import java.text.MessageFormat;

import com.coco.homework.Context;
import com.coco.homework.RomanNumber;
import com.coco.homework.ex.RuleInvalidException;

public class RomanNumberDRule implements IRomanNumberRule {

	@Override
	public void exec(Context context) {
		char ch = context.getCurrentChar();
		RomanNumber romanNumber = RomanNumber.of(ch);
		if (!romanNumber.equals(getCanHandlerNumber())) {
			throw new RuleInvalidException("RomanNumber ERROR ");
		}

		boolean repeatable = notRepeatRuleCheck(context.getLeftString(), ch);
		if (!repeatable) {
			throw new RuleInvalidException(MessageFormat.format(CHAR_REPEAT_MORE, ch, 1));
		}
		context.addTokenVal(romanNumber.getVal());
	}

	@Override
	public RomanNumber getCanHandlerNumber() {
		return RomanNumber.D;
	}
}
